<?php declare(strict_types=1);

/**
 * @license Apache 2.0
 */

namespace OpenApi\Annotations;

/**
 * @Annotation
 * A "Security Scheme Object":
 * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
 */
class SecurityScheme extends AbstractAnnotation
{
    /**
     * $ref See http://json-schema.org/latest/json-schema-core.html#rfc.section.7
     *
     * @var string
     */
    public $ref = UNDEFINED;

    /**
     * The key into OpenApi->security array.
     *
     * @var string
     */
    public $securityScheme = UNDEFINED;

    /**
     * The type of the security scheme.
     *
     * @var string
     */
    public $type = UNDEFINED;

    /**
     * A short description for security scheme.
     *
     * @var string
     */
    public $description = UNDEFINED;

    /**
     * The name of the header or query parameter to be used.
     *
     * @var string
     */
    public $name = UNDEFINED;

    /**
     * Required The location of the API key.
     *
     * @var string
     */
    public $in = UNDEFINED;

    /**
     * The flow used by the OAuth2 security scheme.
     *
     * @var Flow[]
     */
    public $flows = UNDEFINED;

    /**
     * A hint to the client to identify how the bearer token is formatted. Bearer tokens are usually generated by an
     * authorization server, so this information is primarily for documentation purposes.
     *
     * @var string
     */
    public $bearerFormat = UNDEFINED;

    /**
     * The name of the HTTP Authorization scheme.
     *
     * @see https://tools.ietf.org/html/rfc7235#section-5.1
     * @var string
     */
    public $scheme = UNDEFINED;

    /**
     * OpenId Connect URL to discover OAuth2 configuration values. This MUST be in the form of a URL.
     *
     * @var string
     */
    public $openIdConnectUrl = UNDEFINED;

    /**
     * @inheritdoc
     */
    public static $_required = ['securityScheme', 'type'];

    /**
     * @inheritdoc
     */
    public static $_types = [
        'type' => ['http', 'apiKey', 'oauth2', 'openIdConnect'],
        'description' => 'string',
        'name' => 'string',
        'bearerFormat' => 'string',
        'in' => ['query', 'header', 'cookie'],
    ];

    /**
     * @inheritdoc
     */
    public static $_nested = [
        Flow::class => ['flows', 'flow'],
    ];

    /**
     * @inheritdoc
     */
    public static $_parents = [
        Components::class,
    ];

    /**
     * @inheritdoc
     */
    public function merge($annotations, $ignore = false)
    {
        parent::merge($annotations, $ignore);

        if ($this->type === 'oauth2') {
            $this->name = UNDEFINED;
        }
    }
}
